package com.czz.portal.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.czz.common.exception.ResultCodeEnum;
import com.czz.common.vo.R;
import com.czz.portal.entity.Course;
import com.czz.portal.entity.Period;
import com.czz.portal.query.CourseQuery;
import com.czz.portal.service.CourseService;
import com.czz.portal.vo.CourseVo;
import com.czz.portal.vo.PeriodVo;
import com.czz.portal.vo.VideoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @ClassName: CourseController
 * @Author Czz
 * @Date 2020/2/6
 * @Time 13:24
 **/
@RestController
@RequestMapping(value = "/course")
public class CourseController {

    @Autowired
    CourseService courseService;

    /**
    * @Description 获取首页畅销好课
    * @Param []
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/2/6
    * @Time 16:38
    **/
    @GetMapping(value = "/recommend")
    public R getRecommendCourse(){
        List<Course> recommendCourseList = courseService.getRecommendCourseList();
        return R.ok().data("items",recommendCourseList);
    }

    /**
    * @Description 获取首页新上好课
    * @Param []
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/2/6
    * @Time 16:53
    **/
    @GetMapping(value = "/new")
    public R getNewCourse(){
        List<Course> newCourseList = courseService.getNewCourseList();
        return R.ok().data("items",newCourseList);
    }

    /**
    * @Description 获取首页入门课程
    * @Param []
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/2/6
    * @Time 16:53
    **/
    @GetMapping(value = "/guide")
    public R getGuideCourse(){
        List<Course> guideCourseList = courseService.getGuideCourseList();
        return R.ok().data("items",guideCourseList);
    }

    /**
    * @Description 获取首页中高级课程
    * @Param []
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/2/6
    * @Time 16:54
    **/
    @GetMapping(value = "/improve")
    public R getImproveCourse(){
        List<Course> ImproveCourseList = courseService.getImproveCourseList();
        return R.ok().data("items",ImproveCourseList);
    }

    /**
    * @Description 获取首页前沿技术课程
    * @Param []
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/2/6
    * @Time 16:54
    **/
    @GetMapping(value = "/technology")
    public R getTechnologyCourse(){
        List<Course> technologyCourseList = courseService.getTechnologyCourseList();
        return R.ok().data("items",technologyCourseList);
    }

    /**
    * @Description 根据课程id获取课程详情
    * @Param [id]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/2/7
    * @Time 11:29
    **/
    @GetMapping(value = "/detail/{id}")
    public R getCourseDetailById(@PathVariable String id){
        if (StringUtils.isEmpty(id)){
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }
        CourseVo courseVo = courseService.getCourseDetailById(id);
        return R.ok().data("item",courseVo);
    }

    /**
    * @Description 根据课程id获取推荐课程
    * @Param [id]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/2/8
    * @Time 13:18
    **/
    @GetMapping(value = "/recommend/{id}")
    public R getRecommendCourseById(@PathVariable String id){
        if (StringUtils.isEmpty(id)){
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }
        List<CourseVo> recommendCourseList = courseService.getRecommendCourseListById(id);
        return R.ok().data("items",recommendCourseList);
    }


    /**
    * @Description 分页查询所有课程列表
    * @Param [page, limit, courseQuery]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/2/8
    * @Time 13:31
    **/
    @GetMapping(value = "/{page}/{limit}")
    public R getCoursePageList(@PathVariable Long page, @PathVariable Long limit, CourseQuery courseQuery){
        System.out.println(courseQuery.toString());
        if (page <= 0 || limit <= 0){
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }
        Page<Course> pageParam = new Page<>(page, limit);
        courseService.getCoursePageList(pageParam, courseQuery);
        List<Course> records = pageParam.getRecords();
        Long total = pageParam.getTotal();
        return R.ok().data("total", total).data("rows", records);
    }

    /**
    * @Description 根据课时id获取课时详情
    * @Param [id]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/2/17
    * @Time 11:55
    **/
    @GetMapping(value = "/period/{id}")
    public R getPeriodByPeriodId(@PathVariable String id){
        if (StringUtils.isEmpty(id)){
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }
        Period period = courseService.getPeriodByPeriodId(id);
        return R.ok().data("item",period);
    }

    /**
    * @Description 根据章节id判断课程是否免费
    * @Param [id]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/2/24
    * @Time 10:02
    **/
    @GetMapping(value = "/chapter/{id}")
    public R getCourseIsFree(@PathVariable String id){
        if (StringUtils.isEmpty(id)){
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }
        Boolean flag = courseService.getCourseIsFree(id);
        return R.ok().data("item",flag);
    }

    /**
    * @Description 根据视频id查询课时信息
    * @Param [id]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/3/9
    * @Time 10:47
    **/
    @GetMapping(value = "/period/video/{id}")
    public R getPeriodByVideoId(@PathVariable String id){
        if (StringUtils.isEmpty(id)){
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }
        PeriodVo periodVo = courseService.getPeriodByVideoId(id);
        return R.ok().data("item",periodVo);
    }

    /**
    * @Description 根据视频id查询所属课程课时信息
    * @Param [id]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/3/29
    * @Time 12:13
    **/
    @GetMapping(value = "/video/{id}")
    public R getPeriodVideoByVideoId(@PathVariable String id){
        if (StringUtils.isEmpty(id)){
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }
        List<VideoVo> periodList = courseService.getPeriodVideoByVideoId(id);
        return R.ok().data("items",periodList);
    }
}
